<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Auth;
class CommonController extends Controller {
    /*
    *自动执行的方法
    */
    public function _initialize()
    {
        //组建当前访问路径
        $path = CONTROLLER_NAME.'/'.ACTION_NAME;
        //判断是否有登录会话
        if (!$_SESSION['aid']) {
            redirect(U('admin/login/index'));
        } else {
            if ($path != 'Index/index') {
                if ($_SESSION['aid'] != 1) {
                    $auth=new Auth();
                    if (!$auth->check($path, $_SESSION['aid'])) {
                        redirect(U('admin/close/close'));
                    };
                }
            }
        }
    }
    //根据用户权限显示后台页面导航菜单
    public function navAuth()
    {
        $res = M('auth_group_access agc')->join('join auth_group ag on agc.group_id=ag.id join auth_rule ar on find_in_set(ar.id,ag.rules) join auth_category ac on ar.category_id=ac.id')->where('agc.uid='.$_SESSION['aid'].' and second_dir=1')->order('ac.ctitle,ar.title')->field('agc.uid,ac.ctitle,ar.title,ar.name')->select();
        foreach ($res as $value) {
            $ctitle = $value['ctitle'];
            if ($value['ctitle'] == $ctitle) {
                $title = $value['title'];
                if ($value['title'] == $title) {
                    $cate_arr[$ctitle][$title][] = $value['name'];
                }
            }
        }
        return $cate_arr;
    }
    //获取分页
    public function getPage($table = '', $pid = '', $path = '', $pval = 5, $desc = false)
    {
        if (is_numeric($pid)) {
            $pid = abs($pid);
        } else {
            $pid = 1;
        }

        $sql = 'select * from '.$table;
        $result = M()->query($sql);
        $count = count($result);// 查询记录总数

        $startpage = ($pid-1)*$pval;// 分页起始位置
        if ($desc == true) {
            $sql = $sql.' order by id desc limit '.$startpage.','.$pval;// 拼接分页语句
        } else {
            $sql = $sql.' limit '.$startpage.','.$pval;// 拼接分页语句
        }
        $page_list = M()->query($sql);//获取分页数据

        $allpages = ceil($count / $pval);// 获取总页面数
        if ($path != '') {
            foreach ($page_list as $plk => $plv) {
                $url[$plk] = U($path, array('id'=>$plv['id']));// 生成数据修改页面的url
            }
            $dataarr = ['count'=>$count,'allpages'=>$allpages,'pid'=>$pid,'page_list'=>$page_list,'url'=>$url];
        } else {
            $dataarr = ['count'=>$count,'allpages'=>$allpages,'pid'=>$pid,'page_list'=>$page_list];
        }


        return $dataarr;
    }
    //增加数据
    public function adddata($table = '', $add = '')
    {
        $res = M($table)->add($add);
        if ($res) {
            $info = array('type'=>1, 'info'=>'增加成功', 'adddata'=>$add, 'id'=>$res);
        } else {
            $info = array('type'=>0, 'info'=>'增加失败');
        }
        return $info;
    }
    //编辑数据
    public function editdata($table = '', $edit = '', $where = '')
    {
        $res = M($table)->where($where)->setField($edit);
        if ($res) {
            $info = array('type'=>1,'info'=>'编辑成功', 'editdata'=>$edit);
        } else {
            $info = array('type'=>0,'info'=>'编辑失败');
        }
        return $info;
    }
    //删除数据
    public function deldata($table = '', $del = '')
    {
        $sql = 'delete from '.$table.' where id in('.$del.')';
        $res = M()->execute($sql);
        return $res;
    }
    //记录日志
    public function log($content, $more = null, $type = 1)
    {
        $data['type']=$type;
        $data['content']=$content;
        if ($more != null) {
            $data['more']=serialize($more);
        }
        $data['aname']=$_SESSION['aname'];
        $data['ip']=get_client_ip();
        $data['time']=time();
        M('log')->add($data);
    }
}